package com.ruoyi.project.module.purchases.service;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.constant.ErpEnums;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.module.products.domain.Products;
import com.ruoyi.project.module.products.mapper.ProductsMapper;
import com.ruoyi.project.module.products.service.ProductsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.module.purchases.mapper.PurchasesMapper;
import com.ruoyi.project.module.purchases.domain.Purchases;
import com.ruoyi.project.module.purchases.service.IPurchasesService;
import com.ruoyi.common.support.Convert;
import org.springframework.transaction.annotation.Transactional;

import static com.ruoyi.common.utils.StringUtils.IntegerToNum;
import static com.ruoyi.common.utils.StringUtils.stringToNum;

/**
 * 国内采购 服务层实现
 *
 * @author lcx
 * @date 2018-09-05
 */
@Service
@Transactional
public class PurchasesServiceImpl implements IPurchasesService {
    @Autowired
    private PurchasesMapper purchasesMapper;

    @Autowired
    private ProductsServiceImpl productsService;

    /**
     * 查询国内采购信息
     *
     * @param id 国内采购ID
     * @return 国内采购信息
     */
    @Override
    public Purchases selectPurchasesById(Integer id) {
        return purchasesMapper.selectPurchasesById(id);
    }

    /**
     * 查询国内采购列表
     *
     * @param purchases 国内采购信息
     * @return 国内采购集合
     */
    @Override
    public List<Purchases> selectPurchasesList(Purchases purchases, Integer state) {


        //如果是国内采购
        if (state == ErpEnums.Delivered_state.WFH.getValue()) {
            //
            purchases.setChinaReceived(String.valueOf(ErpEnums.Delivered_state.WFH.getValue()));
            //用了搜索框走这里
            if (StringUtils.isNotEmpty(purchases.getSearchValue())) {
                String[] searchRow = {
                        "p2.mainSKU",
                        "p2.productName",
                        "p.chinaexpressno"
                };
                return purchasesMapper.selectPurchasesListAsSearchRow(purchases, searchRow);
            } else {
                return purchasesMapper.selectPurchasesList(purchases);
            }
        }
        //如果是发往海外仓
        else if (state == ErpEnums.Delivered_state.YFH.getValue()) {
            //用了搜索框走这里
            if (StringUtils.isNotEmpty(purchases.getSearchValue())) {
                String[] searchRow = {
                        "p2.productName",
                        "p2.mainSKU",
                        "p2.EAN",
                        "p.chinaexpressno",
                        "p.OverseasExpressNo"

                };
                return purchasesMapper.selectPurchasesOverseasListAsSearchRow(purchases, searchRow);
            } else {

                return purchasesMapper.selectPurchasesOverseasList(purchases);
            }
        }

        //如果是海外仓签收则走带分组函数的sql

        //如果是海外仓签收
        else if (state == ErpEnums.Delivered_state.YQS.getValue()) {
            //用了搜索框走这里
            if (StringUtils.isNotEmpty(purchases.getSearchValue())) {
                String[] searchRow = {
                        "p.OverseasExpressNo"
                };
                return purchasesMapper.selectPurchasesListReceivedAsSearchRow(purchases, searchRow);
            } else {
                return purchasesMapper.selectPurchasesListAsReceived(purchases);
            }
        }

        return null;
    }

    /**
     * @Author lcx
     * @Date 2018/9/26 11:16
     * 合计
     */
    @Override
    public List<Purchases> selectSumCountPurchasesList(Purchases purchases, Integer state) {
        //如果是国内采购
        if (state == ErpEnums.Delivered_state.WFH.getValue()) {
            //
            purchases.setChinaReceived(String.valueOf(ErpEnums.Delivered_state.WFH.getValue()));
            //用了搜索框走这里
            if (StringUtils.isNotEmpty(purchases.getSearchValue())) {
                String[] searchRow = {
                        "p2.productName",
                        "p2.mainSKU",
                        /*"p2.size",*/
                        "p.chinaexpressno"
                };
                return purchasesMapper.selectPurchasesListAsSearchRowAsSum(purchases, searchRow);
            } else {
                return purchasesMapper.selectPurchasesListAsSum(purchases);
            }
        }
        //如果是发往海外仓
        else if (state == ErpEnums.Delivered_state.YFH.getValue()) {
            //用了搜索框走这里
            if (StringUtils.isNotEmpty(purchases.getSearchValue())) {
                String[] searchRow = {
                        "p2.productName",
                        "p2.mainSKU",
                        /*"p2.size",*/
                        "p.chinaexpressno",
                        "p.OverseasExpressNo",
                        "p2.EAN"
                };
                return purchasesMapper.selectPurchasesListAsSearchRowAsSum(purchases, searchRow);
            } else {

                return purchasesMapper.selectPurchasesListAsSum(purchases);
            }
        }

        //如果是海外仓签收则走带分组函数的sql

        //如果是海外仓签收
        else if (state == ErpEnums.Delivered_state.YQS.getValue()) {
            //用了搜索框走这里
            if (StringUtils.isNotEmpty(purchases.getSearchValue())) {
                String[] searchRow = {
                        "p.OverseasExpressNo"
                };
                return purchasesMapper.selectPurchasesListReceivedAsSearchRowAsSum(purchases, searchRow);
            } else {
                return purchasesMapper.selectPurchasesListAsReceivedAsSum(purchases);
            }
        }

        return purchasesMapper.selectPurchasesListAsSum(purchases);
    }

    /**
     * 新增国内采购
     *
     * @param purchases 国内采购信息
     * @return 结果
     */
    @Override
    public int insertPurchases(Purchases purchases) {

        int re_tem = 0;
        //商品id 集合
        String[] productIds = purchases.getProductIdList_Tem();
        //采购数量 集合
        String[] PurchaseQuantitys = purchases.getPurchaseQuantityList_Tem();


        for (int i = 0; i < productIds.length; i++) {
            purchases.setProductId(productIds[i]);
            purchases.setPurchaseQuantity(Integer.valueOf(PurchaseQuantitys[i]));
            re_tem += purchasesMapper.insertPurchases(purchases);
            //更新库存信息 采购数量
            productsService.uploadProductAsidAndType(productIds[i],Integer.valueOf(PurchaseQuantitys[i]),"PurchaseQuantity");
        }

        return re_tem;
    }

    /**
     * 修改国内采购
     *
     * @param purchases 国内采购信息
     * @return 结果
     */
    @Override
    public int updatePurchases(Purchases purchases) {
        Purchases purchases_tem = purchasesMapper.selectPurchasesById(purchases.getId());
        /*计算库存*/
        String productid =purchases_tem.getProductId();
        int num = purchases.getPurchaseQuantity() - purchases_tem.getPurchaseQuantity();

        productsService.uploadProductAsidAndType(productid, num,"PurchaseQuantity");

        return purchasesMapper.updatePurchases(purchases);
    }

    /*发往海外仓*/
    @Override
    public int updatePurchasesOverseas(Purchases purchases) {
        return purchasesMapper.updatePurchases(purchases);
    }

    /*批量发往海外仓*/
    @Override
    public int updatePurchases(String[] ids, Purchases purchases) {

        int flag = 0;

        for (String id : ids) {
            purchases.setId(Integer.valueOf(id));
            flag += purchasesMapper.updatePurchases(purchases);
        }
        return flag;
    }

    /**
     * 删除国内采购对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePurchasesByIds(String ids) {

        //恢复库存数量
        /*计算库存*/
        String[] id = ids.split(",");
        for(String tem:id) {
            Purchases purchases_tem = purchasesMapper.selectPurchasesById(Integer.valueOf(tem));
            String productid = purchases_tem.getProductId();
            int num = purchases_tem.getPurchaseQuantity() * -1;

            productsService.uploadProductAsidAndType(productid, num, "PurchaseQuantity");
        }

        return purchasesMapper.deletePurchasesByIds(Convert.toStrArray(ids));
    }

    /*发往海外仓批量打印
     * */
    @Override
    public List<Purchases> selectProductAsPrintLabel(String ids) {

        List<Purchases> purchasesList = new ArrayList<>();

        String id[] = ids.split(",");

        for (String id_tem : id) {
            Purchases purchases =purchasesMapper.selectProductAsPrinsetLabel(id_tem);

            for (int i=0; i<purchases.getPurchaseQuantity(); i++){
                purchasesList.add(purchases);
            }
        }


        return purchasesList;
    }

    /**
     * @Author lcx
     * @Date 2018/9/11 10:07
     * 查询海外仓签收的货物信息
     */
    @Override
    public List<Purchases> selectPurchasesListAsEditReceived(Purchases purchases) {
        return purchasesMapper.selectPurchasesList(purchases);
    }

    /*海外签收功能*/
    @Override
    public int updatePurchasesAsReceived(Purchases purchases, String[] ids, String[] overseasReceiveQuantity) {

        int retem = 0;
        int i=0;

        for (String id : ids){
            Purchases purchases_tem = purchasesMapper.selectPurchasesById(Integer.valueOf(id));
            purchases.setId(Integer.valueOf(id));
            purchases.setOverseasReceiveQuantity(stringToNum(overseasReceiveQuantity[i]));
            //确认数量  损耗等于 采购数量减去 签收数量 如果 > 0 则代表损耗 否则 代表多了一个
            int lost_num = stringToNum(overseasReceiveQuantity[i]) - purchases_tem.getOverseasReceiveQuantity();
            purchases.setOverseasLossQuantity(lost_num);
            retem  += purchasesMapper.updatePurchases(purchases);

            /*修改海外仓  OverseasS  torageLossQuantity  海外仓损耗量*/
            String productid =purchases_tem.getProductId();

            productsService.uploadProductAsidAndType(productid, lost_num,"SubmitQuantity");


            i++;
        }
        return retem;
    }



}
